<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,target-densitydpi=high-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<link href="images/m.css" rel="stylesheet" type="text/css" />
<script src="inc/jquery.min.js" type="text/javascript"></script>
<title>喜力超凡大师</title>
</head>

<body>
<?php 

include_once 'site_config.php';
include_once "ra_global.php";


if(!$_GET['code']){
	echo "<script language=javascript>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".urlencode(SiteUrl.'user.php')."&response_type=code&scope=snsapi_base&state=1#wechat_redirect';</script>";
}
//获取code
$api_url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$_GET['code']."&grant_type=authorization_code";  

$getcode=getToken($api_url); 

$user_obj=json_decode($getcode);
if($user_obj->openid){
	$user=$db->ig2_want('mae_user','openid="'.trim($user_obj->openid).'" and is_delete=0');
	if($user){
		if($user['role_type'] == '1')	$headerstr='<script>location.href="shopowner.php?id='.$user['id'].'";</script>';
		else	$headerstr='<script>location.href="clerk.php?source=user&id='.$user['id'].'";</script>';
		echo $headerstr;
	}else{
		?>
		<img src="images/nouser.jpg" style="width:100%"/>
	<?php }
}else{
	echo "<script language=javascript>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".urlencode(SiteUrl.'user.php')."&response_type=code&scope=snsapi_base&state=1#wechat_redirect';</script>";
}

function getToken($url){
	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
	$row=curl_exec($ch); 
	return $row;
}
?>
<script language="javascript">
function onBridgeReady(){
	WeixinJSBridge.call('hideOptionMenu');
}

if (typeof WeixinJSBridge == "undefined"){
	if( document.addEventListener ){
		document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
	}else if (document.attachEvent){
	document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
	document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
	}
}else{
	onBridgeReady();
}
</script>
</body>
</html>